<?php

namespace backend\modules\supplier\models;

use Yii;

/**
 * 这个模型基于表 "{{%s_factory}}". 生成
 *
 * @property integer $id
 * @property string $name
 * @property integer $supplier_id
 * @property string $contacts
 * @property string $mobile
 * @property string $tel
 * @property string $qq
 * @property string $remark
 * @property integer $enable
 * @property string $address
 * @property string $website
 * @property string $email
 * @property integer $add_time
 */
class Factory extends \common\models\AR {

    public $modelName = '';

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%s_factory}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name', 'contacts'], 'required'],
            [['supplier_id', 'enable', 'add_time'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['contacts', 'qq'], 'string', 'max' => 20],
            [['mobile', 'tel'], 'string', 'max' => 12],
            [['remark', 'address'], 'string', 'max' => 100],
            [['website', 'email'], 'string', 'max' => 30],
            [['website'], 'url'],
            [['email'], 'email'],
            [['enable'], 'default', 'value' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'supplier_id' => 'Supplier ID',
            'contacts' => '联系人',
            'mobile' => '手机',
            'tel' => '固定电话',
            'qq' => 'QQ号',
            'remark' => '备注',
            'enable' => '启用',
            'address' => '地址',
            'website' => '网址',
            'email' => '邮箱',
            'add_time' => '添加时间',
        ];
    }

    public function beforeValidate() {
        if (parent::beforeValidate()) {
            $this->supplier_id = Yii::$app->user->identity->supplier_id;
            $this->add_time = time();
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 获取当前供应商所有的厂家列表
     * @return array Factory
     */
    public static function getAll(){
        return self::find()->where(['supplier_id'=>Yii::$app->user->identity->supplier_id])->all();
    }

}
